package cn.bdqn.dao;

import cn.bdqn.pojo.HrInfo;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 老鱼
 * @Date: 2019/1/5 14:00
 * @Description:
 */
public class HrInfoDaoImpl extends BaseDao implements HrInfoDao {
    @Override
    public List<HrInfo> hrInfoList() {
        List<HrInfo> list=new ArrayList<HrInfo>();
        HrInfo hrInfo=null;
        ResultSet rs=null;
        try {
            String sql="select * from person order by id desc";
            rs=this.exceuteQuery(sql);
            while (rs.next()) {
                hrInfo=new HrInfo();
                hrInfo.setAge(rs.getInt("age"));
                hrInfo.setDepartment(rs.getString("department"));
                hrInfo.setId(rs.getInt("id"));
                hrInfo.setName(rs.getString("name"));
                hrInfo.setRank(rs.getString("rank"));
                hrInfo.setSex(rs.getString("sex"));
                list.add(hrInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int add(HrInfo info) {
        int result=0;
        try {
            StringBuilder sql=new StringBuilder();
            sql.append("insert into person(name,sex,age,`rank`,department)");
            sql.append(" values(?,?,?,?,?)");
            result=this.exceuteUpdate(sql.toString(),
                    info.getName(),
                    info.getSex(),
                    info.getAge(),
                    info.getRank(),
                    info.getDepartment());
        } catch (Exception e) {
            e.printStackTrace();
            result=-1;
        }
        return result;
    }

    @Override
    public int del(int id) {
        try {
            StringBuilder sql=new StringBuilder();
            sql.append("delete from person");
            sql.append(" where id = ?");
            return this.exceuteUpdate(sql.toString(),id);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }
}
